package com.lagou.config;

import com.lagou.handler.MappedStatementBuilder;
import com.lagou.pojo.Configuration;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

public class XMLMapperBuilder {

    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration =configuration;
    }

    public void parse(InputStream inputStream) throws Exception {

        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();

        String namespace = rootElement.attributeValue("namespace");

        List<String> nodeTypeList = configuration.getNodeTypeList();

        //构建mappedStatement
        for (String nodeType : nodeTypeList) {
            MappedStatementBuilder mappedStatementBuilder = new MappedStatementBuilder(configuration, rootElement.elements(nodeType), nodeType, namespace);
            mappedStatementBuilder.build();
        }
    }
}